$(document).ready(function() {
    var oldscroll = 0, newscroll = 0;
    var hdschHeight = $("#hidesearch").outerHeight();
    $(".holder-input").placeholder();
    $(".foodlayer").hover(//鼠标经过弹出商品介绍文字
            function() {
                var sdheight = $(this).find(".areatips_txt").height();
                $(this).find(".areatips").height(sdheight + 20);
                $(this).addClass("on");
            },
            function() {
                $(this).removeClass("on");
            }
    );

    $(".orderinfo-openbtn").click(function() {
        $(".payinfo-hideout").stop();
        var pheight = $(".payinfo-hide").height();
        var pheightout = $(".payinfo-hideout").height();
        if (pheightout <= 48) {
            $(".payinfo-hideout").animate({height: pheight + 'px'}, "500");
        } else {
            $(".payinfo-hideout").animate({height: '48px'}, "500");
        }
    });

    $("input[name='OrderForm[userContactInfoId]']:radio").click(function() {
        if ($(this).val() == '0') {
            $("#newaddress").show();
        } else {
            $("#newaddress").hide();
        }
    });

    $(".hotcontent").mouseover(function() {  //热门商品显示效果
        $(this).siblings(".hotcontent").find(".hotbody").hide();
        $(this).find(".hotbody").show();
    });

    $(window).scroll(function() {  //迷你搜索栏随滚动条弹出/收起
        newscroll = $(this).scrollTop();
        if (oldscroll < newscroll) {
            if (newscroll >= 250) {
                $("#hidesearch").stop().animate({
                    top: '0px',
                }, 100);
            }
        } else {
            if (newscroll < 250) {
                $("#hidesearch").stop().animate({
                    top: '-' + hdschHeight + 'px',
                }, 100);
            }
        }
        setTimeout(function() {
            oldscroll = newscroll;
        }, 1000);
    });

    $(".tags").hover(function() {  //商品详细页面 tag 鼠标经过效果
        $(this).find(".cmicons").addClass("on");
    }, function() {
        $(this).find(".cmicons").removeClass("on");
    });

    $(".cart-open > .commodity,.cart-open > .price").click(function() {  //购物车展开事件
        if ($(this).parents(".cart-open").siblings(".store-list-out").is(":hidden")) {
            $(this).parents(".cart-open").siblings(".store-list-out").show();
        } else {
            $(this).parents(".cart-open").siblings(".store-list-out").hide();
        }
    });
    $("#liulan > .liulan_layer").click(function() {  //浏览记录展开事件
        
        if ($(this).siblings(".liulanlist").is(":hidden")) {
            $(this).siblings(".liulanlist").show();
			$('#liulan').find('.n_b_m_right').css('background','url("/images/v3/31_1.png") no-repeat center');
        } else {
            $(this).siblings(".liulanlist").hide();
			$('#liulan').find('.n_b_m_right').css('background','url("/images/v3/31.png") no-repeat center');
        }
    });
});

function addshadow() {
    var sHeight = $(document).height();
    var sWidth = $(document).width();
    $("body").append("<div id=\"shadowbox\"></div>");
    $("#shadowbox").css({
        "height": sHeight,
        "width": sWidth
    });
}

function ajaxWindow(title, url,apitype) {
    var sHeight = $(document).height();
    var sWidth = $(document).width();
    addshadow();
    $("#append_parent").html("<div id=\"layer_out\"></div>");
    var html = '';
    html += '<div class="layer_header c_b">' + title;
    html += '<a href="javascript:;" onclick="hideWindow();" class="cmicons"></a>';
    html += '</div>';
    html += '<div id="layer_body"><img src="'+baseurl+'/images/loading.gif"/></div>';
    $("#layer_out").append(html);
    $("#layer_out").css({
        "top": 200,
        "left": (sWidth - 500) / 2
    });
    $("#append_parent").show();
    ajaxGet('#layer_body', url, apitype,'html');
    ajaxGet('.n_h_r_sq', url, apitype,'html');
}

function ajaxWindowQuick(title) {
//    var sHeight = $(document).height();
    var sWidth = $(document).width();
    addshadow();
    $("#append_parent").html("<div id=\"layer_out\"></div>");
    var html = '';
    html += '<div class="layer_header c_b">' + title;
    html += '<a href="javascript:;" onclick="hideWindow();" class="cmicons"></a>';
    html += '</div>';
    html += '<div id="layer_body"><img src="'+baseurl+'/images/loading.gif"/></div>';
    $("#layer_out").append(html);
    $("#layer_out").css({
        "top": 200,
        "left": (sWidth - 500) / 2
    });
    $("#append_parent").show();
}

function hideWindow() {
    $("#layer_out").remove("div");
    $("#append_parent").hide();
    $("#shadowbox").remove("div");
}

function ajaxPost(target, url, params, apitype, aptype) {
    var aptype = aptype ? aptype : 'append';
    $.ajax({
        url: baseurl + '/ajax/' + apitype + '/?mod=' + url,
        cache: false,
        type: 'post',
        async: false,
        dataType: 'json',
        data: params,
        success: function(json) {
            if (json.content) {
                if (aptype == 'append') {
                    $(target).append(json.content);
                } else if (aptype == 'html') {
                    $(target).html(json.content);
                }
            }
            if (json.callback) {
                eval(json.callback);
            }
        }
    });
}

function ajaxPostQuick(target, url, params, aptype) {
    var aptype = aptype ? aptype : 'append';
    $.ajax({
        url: baseurl + url,
        cache: false,
        type: 'post',
        async: false,
        dataType: 'json',
        data: params,
        success: function(json) {
            if (json.content) {
                if (aptype == 'append') {
                    $(target).append(json.content);
                } else if (aptype == 'html') {
                    $(target).html(json.content);
                }
            }
            if (json.callback) {
                eval(json.callback);
            }
        }
    });
}

function delCart(t) {
    $(".store-list-box a").click(function() {
        if ($(this).attr("cart-id")) {
//            alert(t);
            var cartids = $(this).attr("cart-id");
            ajaxPost('.store-list-out', 'delUserCart' + '&t=' + t, 'cartid=' + cartids, 'account', 'html');
        }
    });
}

function delAddress(t) {
    $("#addresslist a").click(function() {
        if ($(this).attr("address-id")) {
            var addressids = $(this).attr("address-id");
            ajaxPost('#addresslist', 'delContactInfo' + '&t=' + t, 'addressid=' + addressids, 'account', 'html');
        }
    });
}

function showMessage(msg, hidetime) {
    var sWidth = $(window).width();
    var sHeight = $(window).height();
    $("#messagelayer").append("<div class=\"msgout msg-show\"></div>");
    $(".msg-show").html(msg);
    var msgwidth = $("#messagelayer").width();
    var msgheight = $("#messagelayer").height();
    $("#messagelayer").css('left', (sWidth - msgwidth) / 2);
//    $("#messagelayer").show();
    $("#messagelayer").animate({
        opacity: "show",
        bottom: (sHeight - msgheight) / 2
    }, 500, function() {
        $("#messagelayer").delay(hidetime * 1000).animate({
            opacity: "hide",
        }, 500);
    });
}

function ajaxGet(target, url, apitype, aptype) {
    var aptype = aptype ? aptype : 'append';
    $.ajax({
        url: baseurl + '/ajax/' + apitype + '/?mod=' + url,
        cache: false,
        type: 'get',
        async: false,
        dataType: 'json',
        success: function(json) {
            if (json.content) {
                if (aptype == 'append') {
                    $(target).append(json.content);
                } else if (aptype == 'html') {
                    $(target).html(json.content);
                }
            }
            if (json.callback) {
                eval(json.callback);
            }
        }
    });
}

